package com.chinasofti.学生管理系统;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //创建集合容器对象
        ArrayList<Student> list = new ArrayList<>();
        ol:
        while(true){
            //1.搭建主界面
            System.out.println("------------------欢迎来到学生管理系统----------------------");
            System.out.println("1. 添加学生");
            System.out.println("2. 删除学生");
            System.out.println("3. 修改学生");
            System.out.println("4. 查看学生");
            System.out.println("5. 退出");
            System.out.println("------------------请输入您的选项：----------------------");
            String choice = sc.next();
            switch(choice){
                case "1":
                    //System.out.println("添加学生");
                    addStudent(list);
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleStudent(list);
                    break;
                case "3":
                    //System.out.println("修改学生");
                    updateStudent(list);
                    break;
                case "4":
                    //System.out.println("查看学生");
                    queryStudent(list);
                    break;
                case "5":
                    System.out.println("感谢您的使用");
                    break ol;
                default:
                    System.out.println("您输入的有误");
                    break;
            }
        }
    }
    //删除学生
    public static  void deleStudent(ArrayList<Student>list){
        Scanner sc = new Scanner(System.in);
        System.out.println("输入要删除的学号");
        String sid = sc.next();
        int index = getIndex(list,sid);
        if(index==-1){
            System.out.println("查无信息，请重新录入");
        }else {
            list.remove(index);
            System.out.println("删除成功");
        }
    }
    //修改学生
    public static void updateStudent(ArrayList<Student>list){
        System.out.println("请输入您要修改的学号");
        Scanner sc =new Scanner(System.in);
        String updateSid = sc.next();
        int index = getIndex(list,updateSid);
        if (index==-1) {
            System.out.println("查无信息，请重新录入");
        }else {
            Student stu = list.get(index);
            System.out.println("信息如下姓名："+stu.getName()+"年龄,"+stu.getAge()+",生日："+stu.getBirthday());
            System.out.println("请输入您的姓名:");
            String name = sc.next();
            System.out.println("请输入您的年龄:");
            int age = sc.nextInt();
            System.out.println("请输入您的生日:");
            String birthday = sc.next();
            Student st = new Student(updateSid,name,age,birthday);
            list.set(index,st);
            System.out.println("修改成功");

        }
    }
    //查询学生
    public static void  queryStudent(ArrayList<Student>list){
        //判断集合中是否有数据
        if (list.size()==0){
            System.out.println("无信息，请添加后重新查询");
            return;
        }
        for (int i = 0; i <list.size() ; i++) {
            Student s = list.get(i);
            System.out.println("学号："+s.getSid()+"   "+"姓名："+s.getName()+"   "+"年龄："+s.getAge()+"岁"+"   "+"生日："+s.getBirthday());
        }

    }
    //添加学生的方法
    public static void addStudent(ArrayList<Student> list){
        Scanner sc = new Scanner(System.in);
        //给出要录入的提示信息
        String sid;
        while(true){
            System.out.println("请输入您的学号:");
            sid = sc.next();
            //学号不能重复
            int index = getIndex(list,sid);
            if(index == -1){
                //学号可以使用
                break;
            }
        }
        System.out.println("请输入您的姓名:");;
        String name = sc.next();
        System.out.println("请输入您的年龄:");
        int age = sc.nextInt();
        System.out.println("请输入您的生日:");
        String birthday = sc.next();
        //将数据封装到student对象
        Student stu = new Student(sid,name,age,birthday);
        list.add(stu);
        System.out.println("添加成功！");

    }
    //查询学号在集合中的索引
    public static int getIndex(ArrayList<Student> list, String sid) {
        //假设传入的学号不存在,不在集合中
        int index = -1;
        //遍历找到每一个学生对象
        for (int i = 0;i<list.size();i++){
            Student s = list.get(i);
            //获取学生sid
            String id = s.getSid();
            if(id.equals(sid)){
                //存在，让index变量记录正确的索引位置；
                index = i;
        }
        }
        return index;
    }



}